package com.example.dou2204.frag.mime;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.example.dou2204.R;
import com.example.dou2204.adapter.VideoAdapter;
import com.example.dou2204.baseMVVM.BaseViewFragment;
import com.example.dou2204.databinding.FragmentShoucangBinding;
import com.example.dou2204.entity.BannerEntity;
import com.example.dou2204.entity.VideoEntity;
import com.example.dou2204.entity.VideoRoom;
import com.example.dou2204.roomdb.DataBaseUtils;

import java.util.ArrayList;
import java.util.List;

public class ShoucangFragment extends BaseViewFragment<FragmentShoucangBinding> {
    List<VideoEntity.DataBean> videos = new ArrayList<>();
    VideoAdapter videoAdapter;

    public ShoucangFragment() {
        // Required empty public constructor
    }


    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        videoAdapter = new VideoAdapter(R.layout.item_video,videos);
        binding.rv.setAdapter(videoAdapter);
        binding.rv.setLayoutManager(new GridLayoutManager(getContext(),3));
        query();

        //取消收藏
        videoAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                quxiao(position);
                return true;
            }
        });

    }

    private void quxiao(int position) {
        List<VideoRoom> videoRooms = DataBaseUtils.getInstance().getVideoDao().queryVideoById(SPUtils.getInstance().getString("username"), videos.get(position).getId());
        Log.d("sss", "quxiao: "+videoRooms.size());
        DataBaseUtils.getInstance().getVideoDao().deleteVideo(videoRooms.get(0));
        ToastUtils.showLong("删除成功");
        videos.remove(videos.get(position));
        videoAdapter.notifyDataSetChanged();
    }

    @Override
    protected int bindLayout() {
        return R.layout.fragment_shoucang;
    }

    @Override
    public void onResume() {
        super.onResume();
        query();
    }

    private void query() {
        videos.clear();
        List<VideoRoom> videoRooms = DataBaseUtils.getInstance().getVideoDao().queryVideo(SPUtils.getInstance().getString("username"));
        for (VideoRoom room : videoRooms) {
            Log.d("sss", "query: "+room.getId()+"==="+room.getCaption());
            VideoEntity.DataBean bean = new VideoEntity.DataBean();
            bean.setCaption(room.getCaption());
            bean.setVideopath(room.getVideopath());
            bean.setAuthname(room.getAuthname());
            bean.setHeadpath(room.getHeadpath());
            bean.setVideomainimg(room.getVideomainimg());
            bean.setId(room.getId());
            videos.add(bean);
        }
        videoAdapter.notifyDataSetChanged();
    }
}